--[[
FilePath: \Lua\View\UIActivityCenter\UIActivityList.lua
周期兑换活动入口
--]]

require 'View/UIBase/UIBase'

UIActivityList = UIBase.New(PanelResNames.UIActivityList)
local _M = UIActivityList

_M.hasInit = false
_M.activeId = nil
_M.isClick = false
_M.RoleLihui = nil

function _M.Init(func, id)
    _M.activeId = id
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Backbtn"), self.OnBack)
        self.bg = self:GetChild("bg");
        self.btnActivityTab = {};
        self.tabController = {};
        for i = 1, 4 do
            local btn = self:GetChild("Activity"..i);
            table.insert(self.btnActivityTab, btn);
            table.insert(self.tabController,  btn:GetChild("UIRedDots"):GetController("c1"));
        end
        self.timeCountText = self:GetChild("ActivityTime");
        self.childTimeCountText = {};
        table.insert(self.childTimeCountText, self:GetChild("Activity1Time"));
        table.insert(self.childTimeCountText, self:GetChild("Activity2Time"));
        table.insert(self.childTimeCountText, self:GetChild("Activity3Time"));
        table.insert(self.childTimeCountText, self:GetChild("Activity4Time"));
        self.title = self:GetChild("n9"):GetChild("title");
        self.activeListTableData = G.dataTable["t_activitylist"];
        self.CashLb = self:GetChild("CashLb");
        self.CoinLb = self:GetChild("CoinLb");
        self.lihui = self:GetChild("lihui");
        self.lihui2 = self:GetChild("lihui2");
        self.heroIcon = self:GetChild("HeroIcon");
        self.modelData = G.dataTable[self.activeListTableData[tostring(_M.activeId)].f_ModelName];
        self:addEvent()
        self:InitView()

        _M.hasInit = true
    end
end

function _M:addEvent()
    for i = 1, #self.btnActivityTab do
        --self.btnActivityTab[i].name = i;
        local index = i;
        self.btnActivityTab[i].onClick:Add(function()
            self:btnActivity(index);
        end);
    end
    self:RegisterEvent(Config.EventType.Fresh_Active,self.RefreshActive);
end

function _M:removeEvent()
    for i = 1, #self.btnActivityTab do
        self.btnActivityTab[i].onClick:Clear();
    end
end

function _M:InitView()
    if self.activeId == nil then
        self:OnBack();
    else
        local tableData, serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
        if serverData == nil then
            self:OnBack();
            return;
        end
        self.countDownNum = serverData.endTime - HelperFunc.GetServerTime();
        local activityConfig = self.modelData[tostring(_M.activeId)];
        self.title.text = getLanguage(activityConfig.f_ActivityName);
        self.bg.url = tostring(activityConfig.f_BgPic);
        self.childActId = decodeJsonStrToTabel(activityConfig.f_ChildActivityId);
        self.costItemId = decodeJsonStrToTabel(activityConfig.f_CostItemId);
        local childActPic = decodeJsonStrToTabel(activityConfig.f_ChildActivityPic);
        local childActTitle = decodeJsonStrToTabel(activityConfig.f_ChildActivityTitle);
        self.childCountDownNum = {};
        for i = 1, #self.childActId do
            if i > #self.btnActivityTab then
                break;
            end
            if tonumber(self.childActId[i]) > 0 then
                tableData, serverData = logicMgr.ActiveManager.getActiveData(self.childActId[i]);
                if serverData == nil then
                    self.childCountDownNum[i] = 0;
                else
                    self.childCountDownNum[i] = serverData.endTime - HelperFunc.GetServerTime();
                end
                self.btnActivityTab[i].text = getLanguage(childActTitle[i]);
                if childActPic[i] ~= "" then
                    self.btnActivityTab[i].icon = childActPic[i];
                end
            else
                self.childCountDownNum[i] = 0;
                self.btnActivityTab[i].visible = false;
            end
        end
        self:updateActiveCountDown();
        self:TimeCall(self.updateActiveCountDown, 1000, -1);

        self.CashLb.icon = EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"][tostring(self.costItemId[1])].f_Icon;
        self.CoinLb.icon = EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"]["1"].f_Icon;

        if activityConfig.f_HeroSpine ~= nil then
            if activityConfig.f_HeroSpine[2] and activityConfig.f_HeroSpine[2] == 1 then
                if self.drawWrapper == nil then
                    self.drawWrapper = GoWrapper.New()
                end
                local scale = activityConfig.f_HeroSpine[4];
                resMgr:LoadModelPrefab("Model/Drawing/"..activityConfig.f_Hero, function(go)
                    if(self.RoleLihui~=nil)then
                        destroyImmediate(self.RoleLihui);
                        self.RoleLihui = nil
                    end
                    self.RoleLihui = newObject(go);
                    self.RoleLihui.name = activityConfig.f_Hero;
                    Util.AddAction(self.RoleLihui,"idle1",true,0.5);
                    self.RoleLihui:AddComponent(TCanvas);
                    self.drawWrapper:SetWrapTarget(self.RoleLihui,false);
                    Util.Lua_SetLocalPositionXYZ(self.RoleLihui.transform,0,0,0)
                    Util.Lua_SetLocalScaleXYZ(self.RoleLihui.transform,scale,scale,scale)
                    self.lihui:SetNativeObject(self.drawWrapper);
                end)
            elseif activityConfig.f_HeroSpine[2] and activityConfig.f_HeroSpine[2] == 2 then
                local list = TablePool.Get()
                string_split(activityConfig.f_HeroSpine[1],"/",list)
                AddUIPackageDepend(PanelResNames.UIActivityList,"FUI/"..list[1])
                TablePool.Release(list)
                self.lihui2.url ="ui://"..activityConfig.f_HeroSpine[1];
                Util.PlayAction(self.lihui2.wrapTarget, activityConfig.f_HeroSpine[3], true);
            end
        end
        if activityConfig.f_HeroIcon and activityConfig.f_HeroIcon ~= "" then
            self.heroIcon.url = tostring(activityConfig.f_HeroIcon)
        end
        --self:RefreshView()
    end
end

function _M:RefreshActive()
    if self.activeId == nil or not _M.hasInit then
        self:OnBack();
    else
        self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
        if self.serverData == nil then
            self:OnBack();
            return;
        end
        self:RefreshView();
    end
end

function _M:RefreshView()
    local tableData = nil
    local serverData = nil
    local controllerNum = #self.tabController;
    local selectIndex = 0;
    for i = 1, #self.childActId do
        tableData, serverData = logicMgr.ActiveManager.getActiveData(self.childActId[i]);
        selectIndex = 0;
        if serverData ~= nil and serverData.redDots ~= nil and serverData.redDots then
            selectIndex = 2;
        end
        if i <= controllerNum then
            self.tabController[i].selectedIndex = selectIndex;
        end
    end

    local haveNum = logicMgr.ItemManager.getBagNumByTableID(1);
    self.CoinLb.title = haveNum;
    haveNum = logicMgr.ItemManager.getBagNumByTableID(tonumber(self.costItemId[1]));
    self.CashLb.title = getNumString(haveNum);
end

function _M:updateActiveCountDown()
    if self.countDownNum <= 0 then
        self.timeCountText.text = getLanguage("Sys_ActivityEnd");
    else
        self.timeCountText.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
        self.countDownNum = self.countDownNum - 1;
    end
    for i = 1, #self.childCountDownNum do
        if self.childCountDownNum[i] <= 0 then
            self.childTimeCountText[i].text = getLanguage("Sys_ActivityEnd");
        else
            self.childTimeCountText[i].text = HelperFunc.GetSurpTimeStr(self.childCountDownNum[i]);
            self.childCountDownNum[i] = self.childCountDownNum[i] - 1;
        end
    end
end

--function _M:btnActivity(evt)
function _M:btnActivity(index)
    if self.isClick then
        return;
    end
    if self.childCountDownNum[index] <= 0 then
        UISysTips.AddMsg(getLanguage("Sys_ActivityEnd"))
        return;
    end

    self.isClick = true;
    --local index = tonumber(evt.sender.name);
    local data = self.activeListTableData[tostring(self.childActId[index])];
    if PanelNames[data.f_InterSurface] ~= nil then
        showUI(PanelNames[data.f_InterSurface], nil, data.f_ActivityId);
    else
        logError("活动找不到UI t_activitylist f_InterSurface:" .. tostring(data.f_InterSurface));
    end

    self:TimeCall(self.clickTimeEnd, 1000);  --防止快速点击
end

function _M:clickTimeEnd()
    self:RemoveTimeCall(self.clickTimeEnd)
    self.isClick = false;
end

-- 显示
function _M:OnEnable()
    self:RefreshActive();
end

-- 隐藏
function _M:OnDisable()
    
end

-- 卸载
function _M:OnDestroy()
    self:RemoveTimeCall(self.clickTimeEnd)
    self:removeEvent();
    if(self.RoleLihui~=nil)then
        destroyImmediate(self.RoleLihui);
        self.RoleLihui = nil
    end
    self.isClick = false;
end

return _M;